<?php
namespace app\models;

use yii;
use yii\db\Query;
/**
 *后台角色表
 *
 */
class AdminRole
{
   
    use \app\lib\Redis;

    private $table;
    private $redis;

    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix.'adminrole';
        $this->redis = $this->getInstance();
    }

    /**
     * 获取角色id对应权限
     * @param int $role_id
     * @return array
     */
    public function getRules($role_id)
    {
        $key = 'role_rules';
        $rules = json_decode($this->redis->get($key),true);

        if ($rules) {
            foreach ($rules as $v) {
                if ($v['id'] == $role_id) {
                    return $v;
                }
            }
        } else {
            $db = new Query();
            $re = $db->select('*')->from($this->table)->all();
            $this->redis->setex($key,3600,json_encode($re));
            foreach ($re as $v) {
                if ($v['id'] == $role_id) {
                    return $v;
                }
            }
        }
    }
}
